<div class="modal-header">
    <h5 class="modal-title strong-600 heading-5">{{ translate('Order id')}}: {{ $order->code }}</h5>
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
        <span aria-hidden="true">&times;</span>
    </button>
</div>

@php
    error_reporting(0);
    $status = $order->delivery_status;
    $payment_status = $order->orderDetails->where('seller_id', Auth::user()->id)->first()->payment_status;
@endphp

<div class="modal-body gry-bg px-3 pt-0">
    {{-- <div class="py-4">
        <div class="row gutters-5 text-center aiz-steps">
            <div class="col @if($status == 'pending') active @else done @endif">
                <div class="icon">
                    <i class="las la-file-invoice"></i>
                </div>
                <div class="title fs-12">{{ translate('Order placed')}}</div>
            </div>
            <div class="col @if($status == 'confirmed') active @elseif($status == 'on_delivery' || $status == 'delivered') done @endif">
                <div class="icon">
                    <i class="las la-newspaper"></i>
                </div>
              <div class="title fs-12">{{ translate('Confirmed')}}</div>
            </div>
            <div class="col @if($status == 'on_delivery') active @elseif($status == 'delivered') done @endif">
                <div class="icon">
                    <i class="las la-truck"></i>
                </div>
                <div class="title fs-12">{{ translate('On delivery')}}</div>
            </div>
            <div class="col @if($status == 'delivered') done @endif">
                <div class="icon">
                    <i class="las la-clipboard-check"></i>
                </div>
                <div class="title fs-12">{{ translate('Delivered')}}</div>
            </div>
        </div>
    </div> --}}
    @if (get_setting('product_manage_by_admin') == 0)
    <div class="row mt-5">
        @if($order->payment_type == 'cash_on_delivery')
            <div class="col-lg-4 col-sm-6">
                <div class="form-group">
                    <label>{{ translate('Payment Status')}}</label>
                    <select class="form-control aiz-selectpicker form-control-sm"  data-minimum-results-for-search="Infinity" id="update_payment_status">
                        <option value="unpaid" @if ($payment_status == 'unpaid') selected @endif>{{ translate('Unpaid')}}</option>
                        <option value="paid" @if ($payment_status == 'paid') selected @endif>{{ translate('Paid')}}</option>
                    </select>
                </div>
            </div>
        @endif



        @if ($order->pickup_status === 'unpicked' && $order->delivery_status !== 'cancelled')
            <div class="col-lg-4 col-sm-6">
                <div class="form-group">
                    <label>{{ translate('Pay Password')}}</label>
                    <input type="password" id="pay_password" class="form-control"  value="">
                </div>
            </div>
            <div class="col-lg-4 col-sm-6">
                <div class="form-group">
                    <label>&nbsp;</label>
                    <div class="form-control" style="border:none">
                        <a id="picked_up" href="javascript:void(0)" style="box-sizing: border-box; background-color: var(--primary); color: #fff; border-radius: 3px; line-height: 1.35; padding: 6px 12px;">{{translate('Payment Picked Up')}}</a>
                    <!--<a id="canceled_up" href="javascript:void(0)" style="box-sizing: border-box; margin-left: 6px; color: #888; border-radius: 3px; line-height: 1.35; padding: 6px 12px; border: solid 1px #ccc;">{{translate('Cancel Order')}}</a>-->
                    </div>
                </div>
            </div>
        @elseif ($order->delivery_status !== 'cancelled')
        <div class="col-lg-4 col-sm-6">
            <div class="form-group">
                <label>{{ translate('Delivery Status')}}</label>
                <select @if (  $status == 'delivered') disabled @endif  class="form-control aiz-selectpicker form-control-sm"  data-minimum-results-for-search="Infinity" id="update_delivery_status">
                    <option value="pending"     @if ($status == 'pending') selected @endif>{{translate('Pending')}}</option>
                    <option value="confirmed"   @if ($status == 'confirmed') selected @endif>{{translate('Confirmed')}}</option>
                    <option value="picked_up"   @if ($status == 'picked_up') selected @endif>{{translate('Picked Up')}}</option>
                    <option value="on_the_way"  @if ($status == 'on_the_way') selected @endif>{{translate('On The Way')}}</option>
                     
                    <option value="cancelled"   @if ($status == 'cancelled') selected @endif>{{translate('Cancel')}}</option>
                </select>
             
            </div>
        </div> 
        
        
          <div class="col-lg-4 col-sm-6">  <div class="form-group">
                <label for="update_tracking_code">{{translate('Tracking Code (optional)')}}</label>
                <input type="text" class="form-control" id="update_tracking_code" value="{{ $order->tracking_code }}">
            </div>
            </div>
        @endif
            
    </div>
    @endif

    <div class="card mt-4">
        <div class="card-header">
          <b class="fs-15">{{ translate('Order Summary') }}</b>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-lg-6">
                    <table class="table table-borderless">
                        <tr>
                            <td class="w-50 fw-600">{{ translate('Order Code')}}:</td>
                            <td>{{ $order->code }}</td>
                        </tr>
                        <tr>
                            <td class="w-50 fw-600">{{ translate('Customer')}}:</td>
                            <td>{{ $order->user->is_virtual_user ? substr(json_decode($order->shipping_address)->name, 0, 1) . str_repeat('*', strlen(json_decode($order->shipping_address)->name) - 1) : json_decode($order->shipping_address)->name }}</td>
                        </tr>
                        <tr>
                            <td class="w-50 fw-600">{{ translate('Email')}}:</td>
                            @if ($order->user_id != null)
                                <td>{{ $order->user->is_virtual_user ? substr($order->user->email, 0, 1) . str_repeat('*', strlen($order->user->email) - 1) : $order->user->email }}</td>
                            @endif
                        </tr>
                        <tr>
                            <td class="w-50 fw-600">{{ translate('Shipping address')}}:</td>
                            <td>
                                @php
                                $address = implode(', ', [json_decode($order->shipping_address)->address, json_decode($order->shipping_address)->city, json_decode($order->shipping_address)->postal_code, json_decode($order->shipping_address)->country]);
                                echo $order->user->is_virtual_user ? str_repeat('*', 20) : $address;
                                @endphp
                            </td>
                        </tr>
                        <tr>
                            <td class="w-50 fw-600">{{ translate('Pickup Status')}}:</td>
                            <td>
                                @if ($order->pickup_status === 'picked')
                                    <span class="badge badge-inline badge-success">{{translate(ucfirst($order->pickup_status))}}</span>
                                @else
                                    <span class="badge badge-inline badge-danger">{{translate(ucfirst($order->pickup_status))}}</span>
                                @endif
                            </td>
                        </tr>
                        <tr>
                            <td class="w-50 fw-600">{{ translate('Pickup Amount')}}:</td>
                            <td>{{single_price($order->pickup_amount)}}</td>
                        </tr>
                    </table>
                </div>
                <div class="col-lg-6">
                    <table class="table table-borderless">
                        <tr>
                            <td class="w-50 fw-600">{{ translate('Order date')}}:</td>
                            <td>{{ date('d-m-Y H:i A', $order->date) }}</td>
                        </tr>
                        <tr>
                            <td class="w-50 fw-600">{{ translate('Order status')}}:</td>
                            <td>{{ translate($status) }}</td>
                        </tr>
                        <tr>
                            <td class="w-50 fw-600">{{ translate('Total order amount')}}:</td>
                            <td>{{ single_price($order->grand_total) }}</td>
                        </tr>
                        <tr>
                            <td class="w-50 fw-600">{{ translate('Contact')}}:</td>
                            <td>{{ $order->user->is_virtual_user ? substr(json_decode($order->shipping_address)->phone, 0, 3) . str_repeat('*', strlen(json_decode($order->shipping_address)->phone) - 2) : json_decode($order->shipping_address)->phone }}</td>
                        </tr>
                        <tr>
                            <td class="w-50 fw-600">{{ translate('Payment method')}}:</td>
                            <td>{{ translate(ucfirst(str_replace('_', ' ', $order->payment_type))) }}</td>
                        </tr>
                        @if ($order->tracking_code)
                            <tr>
                                <td class="w-50 fw-600">{{ translate('Tracking code')}}:</td>
                                <td>{{ $order->tracking_code }}</td>
                            </tr>
                        @endif
                    </table>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-lg-9">
            <div class="card mt-4">
                <div class="card-header">
                  <b class="fs-15">{{ translate('Order Details') }}</b>
                </div>
                <div class="card-body pb-0">
                    <table class="table  table-borderless table-responsive">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>{{ translate('Quantity')}}</th>
                                <th>{{ translate('Delivery Type')}}</th>
                                <th>{{ translate('Price')}}</th>
                                @if (addon_is_activated('refund_request'))
                                    <th>{{ translate('Refund')}}</th>
                                @endif
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($order->orderDetails->where('seller_id', Auth::user()->id) as $key => $orderDetail)
                                <tr>
                                    <td>{{ $key+1 }}</td>
                                    <td>
                                        {{ $orderDetail->quantity }}
                                    </td>
                                    <td>
                                        @if ($order->shipping_type != null && $order->shipping_type == 'home_delivery')
                                            {{  translate('Home Delivery') }}
                                        @elseif ($order->shipping_type == 'pickup_point')
                                            @if ($order->pickup_point != null)
                                                {{ $order->pickup_point->getTranslation('name') }} ({{  translate('Pickip Point') }})
                                            @endif
                                        @endif
                                    </td>
                                    <td>{{ $orderDetail->price }}</td>
                                    @if (addon_is_activated('refund_request'))
                                        <td>
                                            @if ($orderDetail->refund_request != null && $orderDetail->refund_request->refund_status == 0)
                                                <b class="text-info">{{  translate('Pending') }}</b>
                                            @elseif ($orderDetail->refund_request != null && $orderDetail->refund_request->refund_status == 2)
                                                <b class="text-success">{{  translate('Rejected') }}</b>
                                            @elseif ($orderDetail->refund_request != null && $orderDetail->refund_request->refund_status == 1)
                                                <b class="text-success">{{  translate('Approved') }}</b>
                                            @elseif ($orderDetail->product->refundable != 0)
                                                <b>{{  translate('N/A') }}</b>
                                            @else
                                                <b>{{  translate('Non-refundable') }}</b>
                                            @endif
                                        </td>
                                    @endif
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        
        
        
        <div class="col-lg-3">
            <div class="card mt-4">
                <div class="card-header">
                  <b class="fs-15">{{ translate('Order Ammount') }}</b>
                </div>
                <div class="card-body pb-0">
                    <table class="table table-borderless">
                        <tbody>
                            <tr>
                                <td class="w-50 fw-600">{{ translate('Subtotal')}}</th>
                                <td class="text-right">
                                    <span class="strong-600">{{ single_price($order->orderDetails->where('seller_id', Auth::user()->id)->sum('price')) }}</span>
                                </td>
                            </tr>
                            <tr>
                                <td class="w-50 fw-600">{{ translate('Shipping')}}</th>
                                <td class="text-right">
                                    <span class="text-italic">{{ single_price($order->orderDetails->where('seller_id', Auth::user()->id)->sum('shipping_cost')) }}</span>
                                </td>
                            </tr>
                            <tr>
                                <td class="w-50 fw-600">{{ translate('Tax')}}</th>
                                <td class="text-right">
                                    <span class="text-italic">{{ single_price($order->orderDetails->where('seller_id', Auth::user()->id)->sum('tax')) }}</span>
                                </td>
                            </tr>
                            <tr>
                                <td class="w-50 fw-600">{{ translate('Coupon')}}</th>
                                <td class="text-right">
                                    <span class="text-italic">{{ single_price($order->coupon_discount) }}</span>
                                </td>
                            </tr>
                            <tr>
                                <td class="w-50 fw-600">{{ translate('Total')}}</th>
                                <td class="text-right">
                                    <strong>
                                        <span>{{ single_price($order->grand_total) }}
                                        </span>
                                    </strong>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    
     @if( $express->express_info ) 
<style>
    .table {
  border-collapse: collapse;
}

.table, th, td {
  border: 1px solid #edeff3;
}

</style>
                     <table class="table">
                        <tr>
                            <td colspan="2" style="text-align:center;"><b>物流信息</b> </td>
                        </tr>
                        <tr>
                            <td style="width:100px;"> 物流公司：</td>
                            <td>  {{ $express->express_name }} </td>
                        </tr>
                        
                        <tr>
                            <td> 物流单号：</td>
                            <td>  {{ $express->express_code }} </td>
                        </tr>
                        
                      
                        <tr>
                            <td colspan="2" style="text-align:center;"><b>物流信息</b></td>
                            
                        </tr>
                        
                         @if( $express->express_info ) 
                               
                                @foreach ($express->express_info as $key => $ex )
                                 <?php  
                               
                                 if( strtotime( $express->express_time[$key] ) < time()   ){ ?>
                          <tr><td colspan="2">     {{ $express->express_stime[$key] }} {{ $ex }}  </td></tr>
                                <?php  } ?>
                              @endforeach
                              
                              
                              @endif
                              
                     
                        
                    </table>
                    @endif
        
</div>

<script type="text/javascript">
    $('#picked_up').bind('click', function() {
        fetch('{{ route('orders.picked_order') }}', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-Requested-With': 'fetch',
            },
            body: JSON.stringify({
                _token: '{{csrf_token()}}',
                order_id: '{{$order->id}}',
                pay_password: $('#pay_password').val()
            })
        }).then(resp => resp.json()).then(res => {
            if (res.status === 200) {
                AIZ.plugins.notify('success', '{{ translate('Order status has been updated') }}');
            } else {
                AIZ.plugins.notify('danger', res.msg);
            }
            $('#order_details').modal('hide');
            location.reload().setTimeOut(500);
        }).catch(err => null)
    })
     $('#canceled_up').bind('click', function() {
        fetch('{{ route('orders.update_delivery_status') }}', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-Requested-With': 'fetch',
            },
            body: JSON.stringify({
                _token: '{{csrf_token()}}',
                order_id: '{{$order->id}}',
                status: 'cancelled'
            })
        }).then(resp => resp.text()).then(res => {
            $('#order_details').modal('hide');
            AIZ.plugins.notify('success', '{{ translate('Order status has been updated') }}');
            location.reload().setTimeOut(500);
        }).catch(err => null)
    })
    $('#update_delivery_status').on('change', function(){
        var order_id = {{ $order->id }};
        var status = $('#update_delivery_status').val();
        $.post('{{ route('orders.update_delivery_status') }}', {_token:'{{ @csrf_token() }}',order_id:order_id,status:status}, function(data){
            $('#order_details').modal('hide');
            AIZ.plugins.notify('success', '{{ translate('Order status has been updated') }}');
            location.reload().setTimeOut(500);
        });
    });

    $('#update_payment_status').on('change', function(){
        var order_id = {{ $order->id }};
        var status = $('#update_payment_status').val();
        $.post('{{ route('orders.update_payment_status') }}', {_token:'{{ @csrf_token() }}',order_id:order_id,status:status}, function(data){
            $('#order_details').modal('hide');
            //console.log(data);
            AIZ.plugins.notify('success', '{{ translate('Payment status has been updated') }}');
            location.reload().setTimeOut(500);
        });
    });
    
       $('#update_tracking_code').on('change', function(){
            var order_id = {{ $order->id }};
            var tracking_code = $('#update_tracking_code').val();
            $.post('{{ route('orders.update_tracking_code') }}', {_token:'{{ @csrf_token() }}',order_id:order_id,tracking_code:tracking_code}, function(data){
                AIZ.plugins.notify('success', '{{ translate('Order tracking code has been updated') }}');
            });
        });
        
        
</script>
